移动端开发之页面静态化.

Posted by Futari on 2021-03-22
Estimated Reading Time 4 Minutes
Words 1k In Total
Viewed Times

使用Freemarker

在pom中加依赖
创建模板文件(模板文件中有四种元素):
1
2
3
4
5
6
7
8
9
10

1.文本,直接输出

2.注释,不会输出

3.插值表达式,即${},使用数据模型中的部分代替输出

4.freemarker指令,名字前面加#予以区分,不输出

FreeMarker文件后缀名为ftl

Freemarker指令

4.1 assign指令

assign指令用于在页面上定义一个变量

(1)定义简单类型

1
2
<#assign linkman="周先生">
联系人:${linkman}

(2)定义对象类型

1
2
<#assign info={"mobile":"13812345678",'address':'北京市昌平区'} >
电话:${info.mobile} 地址:${info.address}

4.2 include指令

include指令用于模板文件的嵌套

(1)创建模板文件head.ftl

1
<h1>黑马程序员</h1>

(2)修改入门案例中的test.ftl,在test.ftl模板文件中使用include指令引入上面的模板文件

1
<#include "head.ftl"/>

4.3 if指令

if指令用于判断

(1)在模板文件中使用if指令进行判断

1
2
3
4
5
<#if success=true>
你已通过实名认证
<#else>
你未通过实名认证
</#if>

(2)在java代码中为success变量赋值

1
map.put("success", true);

在freemarker的判断中,可以使用= 也可以使用==

4.4 list指令

list指令用于遍历

(1)在模板文件中使用list指令进行遍历

1
2
3
<#list goodsList as goods>
商品名称: ${goods.name} 价格:${goods.price}<br>
</#list>

(2)在java代码中为goodsList赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
List goodsList=new ArrayList();

Map goods1=new HashMap();
goods1.put("name", "苹果");
goods1.put("price", 5.8);

Map goods2=new HashMap();
goods2.put("name", "香蕉");
goods2.put("price", 2.5);

Map goods3=new HashMap();
goods3.put("name", "橘子");
goods3.put("price", 3.2);

goodsList.add(goods1);
goodsList.add(goods2);
goodsList.add(goods3);

map.put("goodsList", goodsList);

首先在配置文件中定义文件目录和Spring配置文件中的Configuration配置类

1
2
3
4
5
6
7
8
<bean id="freemarkerConfig" 
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<!--指定模板文件所在目录-->
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<!--指定字符集-->
<property name="defaultEncoding" value="UTF-8" />
</bean>
<context:property-placeholder location="classpath:freemarker.properties"/>

在服务实现类修改代码,因为在套餐增加时就需要去修改静态页面,修改具体方法,注意将方法中的具体功能细化,每一个功能都封装其具体的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
@Autowired
private SetmealDao setmealDao;
@Autowired
private JedisPool jedisPool;

@Value("${out_put_path}")//从属性文件读取输出目录的路径
private String outputpath ;

//新增套餐,同时关联检查组
public void add(Setmeal setmeal, Integer[] checkgroupIds) {
setmealDao.add(setmeal);
Integer setmealId = setmeal.getId();//获取套餐id
this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
//完成数据库操作后需要将图片名称保存到redis
jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

//新增套餐后需要重新生成静态页面
generateMobileStaticHtml();
}

//生成静态页面
public void generateMobileStaticHtml() {
//准备模板文件中所需的数据
List<Setmeal> setmealList = this.findAll();
//生成套餐列表静态页面
generateMobileSetmealListHtml(setmealList);
//生成套餐详情静态页面(多个)
generateMobileSetmealDetailHtml(setmealList);
}

//生成套餐列表静态页面
public void generateMobileSetmealListHtml(List<Setmeal> setmealList) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("setmealList", setmealList);
this.generateHtml("mobile_setmeal.ftl","m_setmeal.html",dataMap);
}

//生成套餐详情静态页面(多个)
public void generateMobileSetmealDetailHtml(List<Setmeal> setmealList) {
for (Setmeal setmeal : setmealList) {
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("setmeal", this.findById(setmeal.getId()));
this.generateHtml("mobile_setmeal_detail.ftl",
"setmeal_detail_"+setmeal.getId()+".html",
dataMap);
}
}

public void generateHtml(String templateName,String htmlPageName,Map<String, Object> dataMap){
Configuration configuration = freeMarkerConfigurer.getConfiguration();
Writer out = null;
try {
// 加载模版文件
Template template = configuration.getTemplate(templateName);
// 生成数据
File docFile = new File(outputpath + "\\" + htmlPageName);
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
// 输出文件
template.process(dataMap, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != out) {
out.flush();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}

如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !